package cn.iocoder.yudao.framework.business.basic.controller;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Nonnull;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  数据查询控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/11/29
 * @see cn.iocoder.yudao.framework.common.pojo.PageParam
 */
@Validated
public interface ExportController<RESP, QUERY> extends PageController<RESP, QUERY> {

    @GetMapping("/export-excel")
    @Operation(summary = "导出 Excel")
    @ApiAccessLog(operateType = EXPORT)
    default void exportExcel(@Valid QUERY reqVO, HttpServletResponse response) throws IOException {
        CommonResult<PageResult<RESP>> page = getPage(1, PageParam.PAGE_SIZE_NONE, reqVO);
        if (page.isSuccess()) {
            Class<RESP> responseClass = resolveResponseClass();
            List<RESP> list = page.getData().getList();
            ExcelUtils.write(response, responseClass.getSimpleName() + System.currentTimeMillis() + ".xls", "数据", responseClass, list);
        }
    }

    @Nonnull
    Class<RESP> resolveResponseClass();

}
